﻿using LD.Host;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace LD.Tool
{
    public partial class ToolDeleteDBFrm : FrmToolBase
    {
        public ToolDeleteDBFrm()
        {
            InitializeComponent();
            this.Load += ToolDeleteDBFrm_Load;
            this.Shown += ToolMesSharpFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
            CheckForIllegalCrossThreadCalls = true;
        }

        private void ToolDeleteDBFrm_Load(object sender, EventArgs e)
        {
            InfoTiem();
        }

        public static Thread thread;
        private void ToolMesSharpFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            if (TB_Tool.Param != null)
            {
                var param = Newtonsoft.Json.JsonConvert.DeserializeObject<DeleteDBInfo>(TB_Tool.Param);
                if (param != null)
                {
                    this.txt_Val.Text = param.txt_Val_out;
                    this.txt_plcAddress.Text = param.txt_plcAddress_out;
                    this.cob_datadays.SelectedItem = param.cob_datadays_out.ToString();
                    this.txt_tiem.Text = string.IsNullOrEmpty(param.targetTime.ToString()) ? DateTime.Now.AddDays(param.cob_datadays_out).Date.AddHours(8).ToString() : param.targetTime.ToString();
                
                     
                }
            }
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            var param = new DeleteDBInfo()
            {
                txt_Val_out = this.txt_Val.Text,
                txt_plcAddress_out = this.txt_plcAddress.Text,
                cob_datadays_out = Convert.ToInt16(this.cob_datadays.SelectedItem),
                targetTime = Convert.ToDateTime(this.txt_tiem.Text),
            };
            TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(param);
      
            
        }

        private void del_db_rad_day_CheckedChanged(object sender, EventArgs e)
        {
            InfoTiem();
        }

        private void InfoTiem()
        {
            //处理已选天数删除的逻辑
            // 计算十五天后早上7点30分的时间
            DateTime targetTime = DateTime.Now.AddDays(Convert.ToInt16(cob_datadays.SelectedItem)).Date.AddHours(8);
            // DateTime targetTime = DateTime.Now.AddDays(15).Date.AddHours(7).AddMinutes(30);
            txt_tiem.Text = targetTime.ToString();
        }

        private void del_db_rad_plc_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void cob_datadays_SelectedIndexChanged(object sender, EventArgs e)
        {
            InfoTiem();
        }
    }
}
